package ru.nsu.ccfit.shumkina.algorithm.graphworks;

import ru.nsu.ccfit.shumkina.algorithm.logworks.Logs;
import ru.nsu.ccfit.shumkina.algorithm.logworks.Session;

/**
 * Created with IntelliJ IDEA.
 * User: Вера
 * Time: 22:11
 * To change this template use File | Settings | File Templates.
 */
public class LogsGraphMaker {
     public static Graph restoreMenuGraph(Logs logs)
     {
         Graph graph = new Graph();

         Session session = logs.getNextSession();
         while (session != null)
         {
             String prev = null;
             for (String p: session.userFullPath)
             {
                 if (prev != null && p.equals(prev))
                     continue;
                 if (graph.pages.containsKey(p))
                 {
                    // do nothing
                 }
                 else
                 {
                    // add page
                    graph.pages.put(p, new Page(p));
                 }
                 if (prev != null && !graph.arcNames.containsKey(prev+" -> "+p))
                 {
                     //add arc

                     graph.arcNames.put(prev+" -> "+p, p);
                     Page curPage = graph.pages.get(p);
                     Page prPage = graph.pages.get(prev);

                     curPage.parents.put(prev, prPage);
                     prPage.children.put(p, curPage);
                 }
                 prev = p;
             }
             session = logs.getNextSession();
         }
         return graph;
     }
}
